﻿using System;

namespace learnCsharp1
{
    public class Solution {
        public void Rotate(int[][] matrix) {
            if (matrix.Length == 1) matrix = matrix;
            for (int j = 0; j < (matrix.Length%2 == 0?matrix.Length/2:matrix.Length+1/2); ++j) {
                for (int i = 0,count = (matrix.Length-2*j)-1,time=(matrix.Length-1); i < count; ++i) {
                    int temp = 0;
                    temp = matrix[j][i+j];
                    matrix[j][i+j] = matrix[time-i-j][j];
                    matrix[time-i-j][j] = matrix[time-j][time-i-j];
                    matrix[time-j][time-i-j] = matrix[i+j][time-j];
                    matrix[i+j][time-j] = temp;
                }
            }
        }
        static void Main(String[] args)
        {
            int[][] matrix = new int[3][];
            matrix[0] = new int[] {1, 2, 3};
            matrix[1] = new int[] {4, 5, 6};
            matrix[2] = new int[] {7, 8, 9};
            Console.WriteLine("测试样例:");
            for (int i = 0; i < matrix.Length; i++)
            {
                for (int j = 0; j < matrix.Length; j++)
                {
                    Console.Write(matrix[i][j]);
                    Console.Write(" ");
                }
                Console.WriteLine();
            }
            new Solution().Rotate(matrix);
            Console.WriteLine("输出结果:");
            for (int i = 0; i < matrix.Length; i++)
            {
                for (int j = 0; j < matrix.Length; j++)
                {
                    Console.Write(matrix[i][j]);
                    Console.Write(" ");
                }
                Console.WriteLine();
            }
        }
    }
}

